package cn.singno.bob.web.listener;

import java.io.FileNotFoundException;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.springframework.web.util.WebUtils;

public class WebRootListener implements ServletContextListener{

	
	private static final Logger logger = Logger.getLogger(WebRootListener.class);
	
	private static String WEB_ROOT = null;
	
	//该方法在ServletContext启动之后被调用，并准备好处理客户端请求  
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		try {
			WEB_ROOT = WebUtils.getRealPath(sce.getServletContext(), "/");
		} catch (FileNotFoundException e) {
			logger.error("获取应用路径失败", e);
			throw new RuntimeException("获取应用路径失败");
		}
	}

	
	//这个方法在ServletContext 将要关闭的时候调用  
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		WEB_ROOT = null;
	}
	
	
	/**
	 * 
	 * <p>描述：获取应用路径</p>
	 * <pre>
	 *    
	 * </pre>
	 * @return
	 */
	public static final String getWebRoot(){
		return WEB_ROOT;
	}
	

}
